// packageB/pages/Goldbeans/Goldbeans.js
var common = require("../../../public.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        signStatus:null,
        resmsgTitle:'',// 签到成功
        remindMsg:'',// 恭喜获得签到金豆奖励
        DayList: [],
        activeIndex: -1, //控制选中的状态
        pro: [],
        isLoading: true, //如果没有数据，默认为true,表设计正在加载中
        page: 0, //默认第一页
        signCount: 0,
        status: 0,
        willAddMoney: 0, //可得
        glodSuc: false
    },
    // 时间戳 
    click_userable() {
        wx.navigateTo({
            url: '/packageD/pages/useableStore/index',
        })
    },
    click_draw() {
        wx.redirectTo({
            url: '/pages/draw/drawList/index',
        })
    },
    beanChargeClick() {
        wx.navigateTo({
            url: '/pages/mySelf/beanCharge/beanCharge',
          })
    },

    time(time) {
        if (!time) {
            return '-'
        }
        return common.dateFormat('yyyy-MM-dd HH:mm:ss', time)
    },
    goBack() {
        const pages = getCurrentPages();
        const prevPage = pages[pages.length - 2]; //上一个页面
        // 从朋友圈进入的时候，页面栈为空，这里需要判断是否有上个页面
        if (prevPage) {
            wx.navigateBack({
                delta: 1
            })
        } else {
            wx.redirectTo({
                url: '/pages/index/index',
            })
        }
    },
    getData(refresh = false) {
        const that = this
        var pages = that.data.page + 1
        common.get('/userDeposit/getuserDepositDetailsGold', {
            userId: this.data.userInfo.id,
            token: this.data.token,
            page: refresh ? 1 : pages
        }, res => {
            // 11到20 支出
            // if(!res.data){
            //   that.setData({
            //     pro:[]
            //   })
            //   return
            // }
            console.log(res, "5555555")

            var numBox = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
            if (res.data) {
                for (var i = 0; i < res.data.length; i++) {
                    var numType = res.data[i].changeType
                    if (numBox.indexOf(numType) != -1) { //包含就是支出
                        res.data[i].numTypes = 'reduce'
                    } else {
                        res.data[i].numTypes = 'add'
                    }
                }
                if (res.status == 200) {
                    if (refresh) {
                        that.setData({
                            pro: res.data,
                            page: pages
                        })
                    } else {
                        that.setData({
                            pro: [...that.data.pro, ...res.data]
                        })
                    }
                }
            }
        }, () => {

            that.setData({
                isLoading: false
            })
        })
    },
    // 签到大按钮
    click_qianDao() {
        for (var i = 0; i < this.data.DayList.length; i++) {
            console.log(this.data.DayList[i].select, "44444")
            if (this.data.DayList[i].select) {

            }
        }
        if (this.data.status == 0) { //代表今日可以签到
            console.log(this.data.status, "55555")
            if (this.data.DayList.findIndex((target) => target.select === true) == -1) {
                var index = 0
                if (this.data.status == 1) return //后台返回的状态是1就不能签到 0可以
                var ids = this.data.DayList.findIndex((item) => {
                    if (!item.select) return item
                })
                this.setData({
                    golodIndex: index
                })
                if (this.data.status == 0 && ids == index) { //没签到
                    this.sign(index)
                }
                return
            }


            for (var i = 0; i < this.data.DayList.length; i++) {
                console.log(this.data.DayList[i].select, "44444")
                if (this.data.DayList[i].select) {
                    var index = i + 1
                }
            }
            if (this.data.status == 1) return //后台返回的状态是1就不能签到 0可以
            var ids = this.data.DayList.findIndex((item) => {
                if (!item.select) return item
            })
            if (this.data.status == 0 && ids == index) { //没签到
                this.sign(index)

            }
        }
    },
    // 提示
    click_tomorrow() {
        common.showToast('今日已签到，明日再来吧')
    },
    click_qian(e) {
        // this.setData({
        //     glodSuc: true,
        //     resmsgTitle:'签到失败',
        //     signStatus:3,
        //     remindMsg:3123123,
        // })
        // this.sign(-1)
        // return;
        console.log(e.currentTarget.dataset, "55555666")
        var index = e.currentTarget.dataset.index //下标

        // console.log(this.data.status)
        if (this.data.status == 1) return //后台返回的状态是1就不能签到 0可以
        var ids = this.data.DayList.findIndex((item) => {
            if (!item.select) return item
        })
        this.setData({
            golodIndex: index
        })
     
        if (this.data.status == 0 && ids == index) { //没签到
            this.sign(index)
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        let capsule = wx.getMenuButtonBoundingClientRect()
        this.setData({
            height: capsule.height,
            top: capsule.top,
        })
        var windowHeight = wx.getSystemInfoSync().windowHeight
        var windowWidth = wx.getSystemInfoSync().windowWidth
        this.setData({
            scroll_height: windowHeight * 750 / windowWidth - 200
        })

       
    },
    onShow(){
        var that = this
        common.getLocalUserInfo(function (success, data) {
            if (success) {
                that.setData({
                    userInfo: data.user,
                    token: data.token,
                    openid: data.openid,

                }, function () {
                    that.getData(true) //明细
                    that.getUserAssets()
                    that.signData() //获取签到数据
                })
            } else {
                that.setData({
                    userInfo: null,
                    token: '',
                })
            }
        }, true)
    },
    // 获取用户签到数据
    sign(index) {
        const that = this
        const tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token
        }
        common.post('/user/sign', tempJson, function (res) {
            if (res.status === 200) {
                if (that.data.DayList[index].select) {
                    return
                }
                that.setData({
                    activeIndex: index
                }, () => {
                    setTimeout(() => {
                        // wx.showToast({
                        //   title: res.msg,
                        //   icon: 'none'
                        // })
                        that.setData({
                            glodSuc: true,
                            resmsgTitle:'签到成功',
                            signStatus:1,
                            remindMsg:'恭喜获得签到金豆奖励',
                        })
                        that.signData() //获取签到数据
                        that.getData(true)
                        that.getUserAssets()
                    }, 700)
                })
            } else if (res.status === 401) {// 请参与一次抽奖后再进行签到，方可得奖励。
                that.setData({
                    glodSuc: true,
                    resmsgTitle:'签到失败',
                    signStatus:2,
                    remindMsg:res.msg,
                })
                // common.showRequestErrMsg(res)
            } else if (res.status === 402) {// 请用金豆在线下店铺付款后，方可得签到奖励。
                that.setData({
                    glodSuc: true,
                    resmsgTitle:'签到失败',
                    signStatus:3,
                    remindMsg:res.msg,
                })
                // common.showRequestErrMsg(res)
            }else{
                common.showRequestErrMsg(res)
            }
        })
    },
    close_know() {
        this.setData({
            glodSuc: false
        })
    },
    to_reward(){

    },
    // 获取用户签到数据
    signData(index) {
        const that = this
        const tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token
        }
        common.post('/user/signData', tempJson, function (res) {
            if (res.status === 200) {
                var ids = res.data.dayList.findIndex((item) => {
                    if (!item.select) return item
                })
                console.log(ids)
                var indexd;
                if (res.data.status == 0) {
                    indexd = -1
                } else {
                    indexd = ids - 1
                }
                console.log(res.data, "蒙古资质")
                console.log(indexd,"sssssssssss")
                that.setData({
                    activeIndex: indexd,
                    DayList: res.data.dayList,
                    signCount: res.data.signCount,
                    status: res.data.status,
                    willAddMoney: res.data.willAddMoney
                })
            } else {
                common.showRequestErrMsg(res)
            }
        })
    },
    getUserAssets() { // 获取余额
        const that = this
        const tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token
        }
        common.post('/userDeposit/getUserAssetsGold', tempJson, function (res) {
            if (res.status === 200) {
                // console.log(that.time(res.data.changeTime))
                that.setData({
                    userAssets: res.data
                })
            } else {
                common.showRequestErrMsg(res)
                that.setData({
                    userAssets: res.data
                })
            }
        })
    },
    // 跳转详情页面
    click_detail(e) {
        console.log(e, "eeee")
        var item = e.currentTarget.dataset.item
        wx.navigateTo({
            url: '/packageB/pages/Golddetail/Golddetail?item=' + JSON.stringify(item),
        })
    },
    onReachBottom: function () {
        this.getData(false) //获取这张会员卡的信息

    },

})